package whu.cn.whushare.ui.common.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import whu.cn.whushare.R;
import whu.cn.whushare.ui.common.LoadingHelper;


public class WebLoadActivity extends Activity {
    private WebView webView;
    private LoadingHelper loadingHelp;
    private String loadUrl;
    private final static String extraTitle = "title", extraUrl = "url";

    public static Intent getStartIntent(Context context, String title, String url) {
        Intent intent = new Intent();
        intent.setClass(context, WebLoadActivity.class);
        intent.putExtra(extraTitle, title);
        intent.putExtra(extraUrl, url);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = LayoutInflater.from(this);
        setContentView(R.layout.webload_main);

        String title = getIntent().getStringExtra(extraTitle);
        loadUrl = getIntent().getStringExtra(extraUrl);
        TextView topTitle = (TextView) findViewById(R.id.top_title);
        topTitle.setText(title);

        ImageView goBack = (ImageView) findViewById(R.id.go_back_btn);
        goBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        ViewGroup webViewParent = (ViewGroup) findViewById(R.id.webview_parent);
        webView = (WebView) findViewById(R.id.webview);

        //WebView在xml文件中设置背景透明无效
        webView.setBackgroundColor(0);
        webView.setWebViewClient(new MyWebViewClient());


        WebSettings webSettings = webView.getSettings();
        //提高渲染优先级
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setUseWideViewPort(false);

        loadingHelp = new LoadingHelper(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                loadUrl();
            }
        });
        loadingHelp.onCreateView(inflater, webViewParent, savedInstanceState, webView);

        loadUrl();
    }

    public void loadUrl() {
        webView.loadUrl(loadUrl);
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    /**
     * 自定义WebView事件
     */
    private class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            loadingHelp.showLoadingView(false);
            loadUrl = url;
            loadUrl();
            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            loadingHelp.showRetryView();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            loadingHelp.showContentView();
        }
    }
}
